<template>
  <div class="dialog">
    <el-dialog :title="'规格' + titleText" :visible.sync="showDialog" width="35%" :close-on-click-modal="false"
      @close="cancel">
      <el-form ref="ruleForm" :model="formData" :rules="rules" label-width="120px" class="demo-ruleForm">
        <el-form-item label="品种:" prop="fishVarietyId">
          <el-select class="w100p" v-model="formData.fishVarietyId" placeholder="请选择品种" filterable clearable>
            <el-option v-for="item in FishVarietyData" :key="item.fishVarietyId" :label="item.varietyName"
              :value="item.fishVarietyId" />
          </el-select>
        </el-form-item>
        <el-form-item label="规格大小：" prop="specificationName"> 
          <el-input v-model="formData.specificationName" clearable placeholder="请输入规格大小 例：1-5斤" />
        </el-form-item>
        <el-form-item label="备注：" prop="remark">
          <el-input v-model="formData.remark" clearable :rows="2" type="textarea" placeholder="请输入描述" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button class="cancelbtn" @click="cancel()">取 消</el-button>
        <el-button type="primary" class="savebtn" @click="save('ruleForm')">保 存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>

import SpecificationApi from "@/api/basic-info-manage/specification"
import FishVarietyApi from "@/api/basic-info-manage/fishVariety"

export default {
  name: '',
  components: {},
  props: {
    // 弹框名称
    titleText: {
      type: String,
      default: ""
    },
    nowChanceStau: {
      type: String,
      default: ""
    },
    show: {
      type: Boolean,
      default: false
    },
    editObject: {
      type: [Object],
      default: () => {
        return {}
      }
    }
  },
  data() {
    return {
      FishVarietyData: [],
      formData: {},
      rules: {
        fishVarietyId: [
          { required: true, message: "请选择品种", trigger: ["change", "blur"] }
        ],
        area: [
          { required: true, message: "请输入规格面积", trigger: ["change", "blur"] }
        ],
        specificationName: [
          { required: true, message: "请输入规格", trigger: ["change", "blur"] }
        ],
      },
      showDialog: this.show
    };
  },
  computed: {
  },
  watch: {
    show(v) {
      this.showDialog = v;
    },
    nowChanceStau(v) {
      if (v === 'add') {
        this.formData = {
          staffName: sessionStorage.getItem('staffName'),
        }
        this.$nextTick(() => {
          this.$refs['ruleForm'].clearValidate()
        })
      }
      if (v === 'edit') {
        this.formData = this.editObject
      }
    }
  },
  created() {
  },
  mounted() {
    this.queryFishVariety()
  },
  methods: {
    queryFishVariety() {
      let params = {
        page: 1,
        size: 1000,
      }
      FishVarietyApi.queryPageByCondition(params).then(res => {
        console.log('---品种查询---', res);
        this.FishVarietyData = res.data.content;
      })
    },
    save(formData) {
      this.$refs[formData].validate(valid => {
        if (valid) {
          if (this.nowChanceStau === "edit") {
            SpecificationApi.edit(this.formData).then(res => {
              if (res.success === true) {
                this.$message.success("修改成功！");
                this.close()
              } else {
                this.$message.error(res.message);
              }
            });
          } else if (this.nowChanceStau === "add") {
            SpecificationApi.add(this.formData).then(res => {
              if (res.success === true) {
                this.$message.success("添加成功！");
                this.close()
              } else {
                this.$message.error(res.message);
              }
            });
          }
        } else {
          return false;
        }
      });
    },
    close() {
      this.$emit("closeDialog");
    },
    cancel() {
      this.$emit("cancelDialog");
    }
  }
};
</script>

<style lang="scss" scoped></style>
